<template>
  <div class="user-management-container">
    <!-- 文档提示区域 -->
    <div class="doc-alerts">
      <doc-alert title="用户体系" url="https://doc.iocoder.cn/user-center/" />
      <doc-alert title="三方登陆" url="https://doc.iocoder.cn/social-user/" />
      <doc-alert title="Excel 导入导出" url="https://doc.iocoder.cn/excel-import-and-export/" />
    </div>

    <el-row :gutter="20" class="main-content">
      <!-- 左侧部门树 -->
      <el-col :span="4" :xs="24" class="dept-tree-col">
        <ContentWrap class="dept-tree-wrapper">
          <div class="tree-header">
            <div class="header-icon">
              <Icon icon="ep:office-building" />
            </div>
            <div class="header-content">
              <h4 class="tree-title">部门结构</h4>
              <p class="tree-subtitle">选择部门筛选用户</p>
            </div>
          </div>
          <div class="tree-container">
            <DeptTree @node-click="handleDeptNodeClick" />
          </div>
        </ContentWrap>
      </el-col>

      <el-col :span="20" :xs="24" class="user-content-col">
        <!-- 搜索区域 -->
        <ContentWrap class="search-wrapper">
          <div class="search-header">
            <div class="header-icon">
              <Icon icon="ep:search" />
            </div>
            <div class="header-content">
              <h4 class="search-title">用户管理</h4>
              <p class="search-subtitle">查询和管理系统用户信息</p>
            </div>
          </div>

          <div class="search-form-container">
            <el-form
              :model="queryParams"
              ref="queryFormRef"
              :inline="true"
              label-width="100px"
              label-position="left"
              class="enhanced-search-form"
            >
              <div class="form-row">
                <el-form-item label="登录账号" prop="username" class="form-item">
                  <el-input
                    v-model="queryParams.username"
                    placeholder="请输入登录账号"
                    clearable
                    @keyup.enter="handleQuery"
                    class="enhanced-input"
                  >
                    <template #prefix>
                      <Icon icon="ep:user" />
                    </template>
                  </el-input>
                </el-form-item>

                <el-form-item label="手机号码" prop="mobile" class="form-item">
                  <el-input
                    v-model="queryParams.mobile"
                    placeholder="请输入手机号码"
                    clearable
                    @keyup.enter="handleQuery"
                    class="enhanced-input"
                  >
                    <template #prefix>
                      <Icon icon="ep:phone" />
                    </template>
                  </el-input>
                </el-form-item>

                <el-form-item label="状态" prop="status" class="form-item">
                  <el-select
                    v-model="queryParams.status"
                    placeholder="用户状态"
                    clearable
                    class="enhanced-select"
                  >
                    <template #prefix>
                      <Icon icon="ep:switch" />
                    </template>
                    <el-option
                      v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    >
                      <div class="option-item">
                        <Icon
                          :icon="dict.value === 0 ? 'ep:check' : 'ep:close'"
                          class="option-icon"
                          :class="dict.value === 0 ? 'success' : 'danger'"
                        />
                        <span>{{ dict.label }}</span>
                      </div>
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="创建时间" prop="createTime" class="form-item date-item">
                  <el-date-picker
                    v-model="queryParams.createTime"
                    value-format="YYYY-MM-DD HH:mm:ss"
                    type="datetimerange"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    class="enhanced-date-picker"
                  >
                    <template #prefix>
                      <Icon icon="ep:calendar" />
                    </template>
                  </el-date-picker>
                </el-form-item>
              </div>
            </el-form>

            <div class="form-actions">
              <div class="action-group search-group">
                <el-button @click="handleQuery" type="primary" class="action-btn search-btn">
                  <Icon icon="ep:search" class="btn-icon" />
                  搜索
                </el-button>
                <el-button @click="resetQuery" class="action-btn reset-btn">
                  <Icon icon="ep:refresh" class="btn-icon" />
                  重置
                </el-button>
              </div>

              <div class="action-group operation-group">
                <el-button
                  type="primary"
                  plain
                  @click="openForm('create')"
                  v-hasPermi="['system:user:create']"
                  class="action-btn add-btn"
                >
                  <Icon icon="ep:plus" class="btn-icon" />
                  新增
                </el-button>
                <el-button
                  type="warning"
                  plain
                  @click="handleImport"
                  v-hasPermi="['system:user:import']"
                  class="action-btn import-btn"
                >
                  <Icon icon="ep:upload" class="btn-icon" />
                  导入
                </el-button>
                <el-button
                  type="success"
                  plain
                  @click="onPayment('system-account')"
                  :loading="exportLoading"
                  class="action-btn buy-btn"
                >
                  <Icon icon="ep:goods-filled" class="btn-icon" />
                  购买账号
                </el-button>
              </div>
            </div>
          </div>
        </ContentWrap>

        <!-- 用户列表区域 -->
        <ContentWrap class="table-wrapper">
          <div class="table-header">
            <div class="header-icon">
              <Icon icon="ep:list" />
            </div>
            <div class="header-content">
              <h4 class="table-title">用户列表</h4>
              <p class="table-subtitle">共 {{ total }} 个用户</p>
            </div>
          </div>

          <div class="table-container">
            <el-table
              v-loading="loading"
              :data="list"
              class="enhanced-table"
              :header-cell-style="{ background: '#f8f9ff', color: '#303133', fontWeight: '600' }"
            >
              <el-table-column
                label="登录账号"
                align="center"
                prop="username"
                width="150"
                :show-overflow-tooltip="true"
              >
              </el-table-column>

              <el-table-column label="企微" align="center" width="200">
                <template #default="scope">
                  <div class="wework-cell">
                    <div v-if="bindRes[scope.row.id]" class="bind-status bound">
                      <Icon icon="ep:check" class="status-icon success" />
                      <span class="status-text">{{ bindRes[scope.row.id]?.weUserId }}</span>
                    </div>
                    <div
                      v-else
                      @click="handleQwBind(scope.row.id, scope.row.nickname)"
                      class="bind-status unbound"
                    >
                      <Icon icon="ep:warning" class="status-icon warning" />
                      <span class="status-text">未绑定</span>
                    </div>
                  </div>
                </template>
              </el-table-column>

              <el-table-column
                label="用户昵称"
                align="center"
                prop="nickname"
                :show-overflow-tooltip="true"
              >
                <template #default="scope">
                  <div class="nickname-cell">
                    <Icon icon="ep:avatar" class="cell-icon" />
                    <span class="cell-text">{{ scope.row.nickname }}</span>
                  </div>
                </template>
              </el-table-column>

              <el-table-column
                label="部门"
                align="center"
                key="deptName"
                prop="deptName"
                :show-overflow-tooltip="true"
              >
                <template #default="scope">
                  <div class="dept-cell">
                    <Icon icon="ep:office-building" class="cell-icon" />
                    <span class="cell-text">{{ scope.row.deptName || '-' }}</span>
                  </div>
                </template>
              </el-table-column>

              <el-table-column label="手机号码" align="center" prop="mobile" width="180">
                <template #default="scope">
                  <div class="mobile-cell">
                    <Icon icon="ep:phone" class="cell-icon" />
                    <span class="cell-text">{{ scope.row.mobile || '-' }}</span>
                  </div>
                </template>
              </el-table-column>

              <el-table-column label="状态" key="status">
                <template #default="scope">
                  <div class="status-cell">
                    <el-switch
                      v-model="scope.row.status"
                      :active-value="0"
                      :inactive-value="1"
                      @change="handleStatusChange(scope.row)"
                    />
                  </div>
                </template>
              </el-table-column>

              <el-table-column
                label="创建时间"
                align="center"
                prop="createTime"
                :formatter="dateFormatter"
                width="200"
              >
              </el-table-column>

              <el-table-column label="操作" align="center" width="300" fixed="right">
                <template #default="scope">
                  <div class="action-cell">
                    <el-button
                      type="primary"
                      link
                      @click="openForm('update', scope.row.id, true)"
                      v-hasPermi="['system:user:update']"
                      class="action-button edit-btn"
                    >
                      <Icon icon="ep:edit" class="btn-icon" />
                      修改
                    </el-button>
                    <el-button
                      type="primary"
                      link
                      @click="handleRole(scope.row)"
                      v-if="checkPermi(['system:permission:assign-user-role'])"
                      class="action-button role-btn"
                    >
                      <Icon icon="ep:circle-check" class="btn-icon" />
                      分配角色
                    </el-button>
                    <el-dropdown
                      class="more-dropdown"
                      @command="(command) => handleCommand(command, scope.row)"
                      v-hasPermi="[
                        'system:user:delete',
                        'system:user:update-password',
                        'system:permission:assign-user-role'
                      ]"
                    >
                      <el-button type="primary" link class="action-button more-btn">
                        <Icon icon="ep:d-arrow-right" class="btn-icon" />
                        更多
                      </el-button>
                      <template #dropdown>
                        <el-dropdown-menu class="enhanced-dropdown-menu">
                          <el-dropdown-item
                            command="handleDelete"
                            v-if="checkPermi(['system:user:delete'])"
                            class="dropdown-item delete-item"
                          >
                            <Icon icon="ep:delete" class="dropdown-icon" />
                            删除
                          </el-dropdown-item>
                          <el-dropdown-item
                            command="handleResetPwd"
                            v-if="checkPermi(['system:user:update-password'])"
                            class="dropdown-item reset-item"
                          >
                            <Icon icon="ep:key" class="dropdown-icon" />
                            重置密码
                          </el-dropdown-item>
                        </el-dropdown-menu>
                      </template>
                    </el-dropdown>
                  </div>
                </template>
              </el-table-column>
            </el-table>

            <!-- 分页 -->
            <div class="pagination-wrapper">
              <Pagination
                :total="total"
                v-model:page="queryParams.pageNo"
                v-model:limit="queryParams.pageSize"
                @pagination="getList"
              />
            </div>
          </div>
        </ContentWrap>
      </el-col>
    </el-row>

    <!-- 添加或修改用户对话框 -->
    <UserForm ref="formRef" @success="getList" />
    <!-- 用户导入对话框 -->
    <UserImportForm ref="importFormRef" @success="getList" />
    <!-- 分配角色 -->
    <UserAssignRoleForm ref="assignRoleFormRef" @success="getList" />
    <!-- 购买套餐规格列表 -->
    <SkuFormDialog ref="skuFormDialog" />
  </div>
</template>

<script lang="ts" setup>
import SkuFormDialog from '@/components/SkuFormDialog/index.vue'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { checkPermi } from '@/utils/permission'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { CommonStatusEnum } from '@/utils/constants'
import * as UserApi from '@/api/system/user'
import UserForm from './UserForm.vue'
import UserImportForm from './UserImportForm.vue'
import UserAssignRoleForm from './UserAssignRoleForm.vue'
import DeptTree from './DeptTree.vue'

defineOptions({ name: 'SystemUser' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  username: undefined,
  mobile: undefined,
  status: undefined,
  deptId: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单

const bindRes = ref({})
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await UserApi.getUserPage(queryParams)
    list.value = data.list
    const ids = []
    list.value.map((i) => {
      ids.push(i.id)
    })
    bindRes.value = await UserApi.getBindStatus(ids)
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const handleQwBind = async (id, nickname) => {
  const data = await UserApi.getAuthUrlByUser(id)
  ElMessageBox.confirm(
    `请通知"${nickname}"的账户自行使用企微扫码或打开授权链接进行绑定`,
    '企微员工绑定提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
      closeOnClickModal: false
    }
  )
    .then(() => {
      window.open(data)
    })
    .catch(() => {})
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

/** 处理部门被点击 */
const handleDeptNodeClick = async (row) => {
  queryParams.deptId = row.id
  await getList()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number, isEdit) => {
  formRef.value.open(type, id, isEdit)
}

/** 用户导入 */
const importFormRef = ref()
const handleImport = () => {
  importFormRef.value.open()
}

/** 修改用户状态 */
const handleStatusChange = async (row: UserApi.UserVO) => {
  try {
    // 修改状态的二次确认
    const text = row.status === CommonStatusEnum.ENABLE ? '启用' : '停用'
    await message.confirm('确认要"' + text + '""' + row.username + '"用户吗?')
    // 发起修改状态
    await UserApi.updateUserStatus(row.id, row.status)
    // 刷新列表
    await getList()
  } catch {
    // 取消后，进行恢复按钮
    row.status =
      row.status === CommonStatusEnum.ENABLE ? CommonStatusEnum.DISABLE : CommonStatusEnum.ENABLE
  }
}

/** 导出按钮操作 */
const exportLoading = ref(false)
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await UserApi.exportUser(queryParams)
    download.excel(data, '用户数据.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 操作分发 */
const handleCommand = (command: string, row: UserApi.UserVO) => {
  switch (command) {
    case 'handleDelete':
      handleDelete(row.id)
      break
    case 'handleResetPwd':
      handleResetPwd(row)
      break
    default:
      break
  }
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await UserApi.deleteUser(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 重置密码 */
const handleResetPwd = async (row: UserApi.UserVO) => {
  try {
    // 重置的二次确认
    const result = await message.prompt(
      '请输入"' + row.username + '"的新密码',
      t('common.reminder')
    )
    const password = result.value
    // 发起重置
    await UserApi.resetUserPwd(row.id, password)
    message.success('修改成功，新密码是：' + password)
  } catch {}
}

/** 分配角色 */
const assignRoleFormRef = ref()
const handleRole = (row: UserApi.UserVO) => {
  assignRoleFormRef.value.open(row)
}

const skuFormDialog = ref()
// 吊起支付方法
const onPayment = (code: string, type?: number = 2) => {
  skuFormDialog.value.open(type, code, null)
}
/** 初始化 */
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$info-color: #909399;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

.user-management-container {
  padding: 0;
  background: $bg-color;
  min-height: 100vh;

  @media (max-width: 768px) {
    padding: 0;
  }
}

/* 文档提示区域 */
.doc-alerts {
  margin-bottom: 16px;

  @media (max-width: 768px) {
    margin-bottom: 12px;
  }

  :deep(.doc-alert) {
    margin-bottom: 8px;
    border-radius: $border-radius-small;
    box-shadow: $shadow-base;
  }
}

/* 主要内容区域 */
.main-content {
  @media (max-width: 768px) {
    .dept-tree-col {
      margin-bottom: 16px;
    }
  }
}

/* 部门树样式 */
.dept-tree-wrapper {
  border-radius: $border-radius;
  overflow: hidden;
  box-shadow: $shadow-light;
  border: 1px solid $border-light;
  height: calc(100vh - 200px);
  display: flex;
  flex-direction: column;

  @media (max-width: 768px) {
    height: auto;
    min-height: 300px;
  }

  :deep(.el-card__body) {
    padding: 0;
    height: 100%;
    display: flex;
    flex-direction: column;
  }
}

.tree-header {
  display: flex;
  align-items: center;
  padding: 16px 20px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;
  flex-shrink: 0;

  @media (max-width: 768px) {
    padding: 12px 16px;
    flex-direction: column;
    gap: 8px;
    text-align: center;
  }
}

.header-icon {
  width: 32px;
  height: 32px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: $border-radius-small;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    width: 28px;
    height: 28px;
    font-size: 14px;
  }
}

.header-content {
  flex: 1;
  margin-left: 12px;

  @media (max-width: 768px) {
    margin-left: 0;
  }
}

.tree-title {
  font-size: 16px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 14px;
  }
}

.tree-subtitle {
  font-size: 12px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 11px;
  }
}

.tree-container {
  flex: 1;
  padding: 16px;
  background: white;
  overflow-y: auto;

  @media (max-width: 768px) {
    padding: 12px;
  }

  /* 滚动条美化 */
  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.05);
    border-radius: 3px;
  }

  &::-webkit-scrollbar-thumb {
    background: rgba(0, 82, 204, 0.3);
    border-radius: 3px;
    transition: all 0.3s ease;

    &:hover {
      background: rgba(0, 82, 204, 0.5);
    }
  }
}

/* 搜索区域样式 */
.search-wrapper {
  margin-bottom: 16px;
  border-radius: $border-radius;
  overflow: hidden;
  box-shadow: $shadow-light;
  border: 1px solid $border-light;

  :deep(.el-card__body) {
    padding: 0;
  }
}

.search-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
    gap: 8px;
    text-align: center;
  }
}

.search-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

.search-subtitle {
  font-size: 13px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

.search-form-container {
  padding: 24px;
  background: white;

  @media (max-width: 768px) {
    padding: 16px;
  }
}

.enhanced-search-form {
  .form-row {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
    margin-bottom: 20px;

    @media (max-width: 768px) {
      flex-direction: column;
      gap: 12px;
      margin-bottom: 16px;
    }
  }

  .form-item {
    flex: 1;
    min-width: 260px;
    margin-bottom: 0;
    align-items: center;
    @media (max-width: 768px) {
      min-width: auto;
      flex: none;
    }

    &.date-item {
      min-width: 300px;

      @media (max-width: 768px) {
        min-width: auto;
      }
    }

    :deep(.el-form-item__label) {
      font-weight: 600;
      color: $text-regular;
      padding-bottom: 8px;

      @media (max-width: 768px) {
        font-size: 14px;
        width: 100% !important;
        text-align: left !important;
        margin-bottom: 8px;
      }
    }

    :deep(.el-form-item__content) {
      @media (max-width: 768px) {
        margin-left: 0 !important;
      }
    }
  }

  // H5适配：表单项垂直布局
  @media (max-width: 768px) {
    :deep(.el-form-item) {
      display: block;
    }
  }
}

/* 输入框样式 */
.enhanced-input {
  width: 100%;

  :deep(.el-input__wrapper) {
    border-radius: $border-radius-small;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;
    padding: 12px 16px;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
      border-color: $primary-color;
    }
  }
}

/* 选择器样式 */
.enhanced-select {
  width: 100%;

  :deep(.el-select__wrapper) {
    border-radius: $border-radius-small;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;
    padding: 8px 16px;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focused {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
      border-color: $primary-color;
    }
  }
}

/* 日期选择器样式 */
.enhanced-date-picker {
  width: 100%;

  :deep(.el-input__wrapper) {
    border-radius: $border-radius-small;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;
    padding: 12px 16px;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
      border-color: $primary-color;
    }
  }
}

/* 选项样式 */
.option-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
}

.option-icon {
  font-size: 14px;

  &.success {
    color: $success-color;
  }

  &.danger {
    color: $danger-color;
  }
}

/* 操作按钮区域 */
.form-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 16px;

  @media (max-width: 768px) {
    flex-direction: column;
    gap: 12px;
  }
}

.action-group {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;

  @media (max-width: 768px) {
    width: 100%;
    justify-content: center;
  }

  &.search-group {
    @media (max-width: 768px) {
      order: 1;
    }
  }

  &.operation-group {
    @media (max-width: 768px) {
      order: 2;
      flex-direction: column;
    }
  }
}

.action-btn {
  border-radius: $border-radius-small;
  padding: 10px 16px;
  font-weight: 500;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;

  &:hover {
    transform: translateY(-1px);
  }

  @media (max-width: 768px) {
    width: 100%;
    justify-content: center;
    padding: 12px 16px;
  }

  .btn-icon {
    font-size: 14px;
  }

  &.search-btn {
    background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
    border: none;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
    }
  }

  &.reset-btn {
    border: 2px solid $border-color;
    color: $text-regular;

    &:hover {
      border-color: $primary-color;
      color: $primary-color;
      background: $primary-light;
    }
  }

  &.add-btn {
    border: 2px solid $primary-color;
    color: $primary-color;

    &:hover {
      background: $primary-color;
      color: white;
    }
  }

  &.import-btn {
    border: 2px solid $warning-color;
    color: $warning-color;

    &:hover {
      background: $warning-color;
      color: white;
    }
  }

  &.buy-btn {
    border: 2px solid $success-color;
    color: $success-color;

    &:hover {
      background: $success-color;
      color: white;
    }
  }
}

/* 表格区域样式 */
.table-wrapper {
  border-radius: $border-radius;
  overflow: hidden;
  box-shadow: $shadow-light;
  border: 1px solid $border-light;

  :deep(.el-card__body) {
    padding: 0;
  }
}

.table-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
    gap: 8px;
    text-align: center;
  }
}

.table-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

.table-subtitle {
  font-size: 13px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

.table-container {
  background: white;
  overflow-x: auto;
}

.enhanced-table {
  :deep(.el-table__header) {
    th {
      background: linear-gradient(135deg, #f8f9ff 0%, #f0f2ff 100%) !important;
      color: $text-primary !important;
      font-weight: 600 !important;
      border-bottom: 2px solid $primary-light;
      padding: 16px 12px;
    }
  }

  :deep(.el-table__body) {
    tr {
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light !important;
      }

      td {
        padding: 14px 12px;
        border-bottom: 1px solid $border-light;
      }
    }

    .el-table__row--striped {
      background: #fafbff;

      &:hover {
        background: $primary-light !important;
      }
    }
  }

  // H5适配
  @media (max-width: 768px) {
    :deep(.el-table__header) th {
      padding: 12px 8px;
      font-size: 13px;
    }

    :deep(.el-table__body) td {
      padding: 12px 8px;
      font-size: 12px;
    }
  }
}

/* 表格单元格样式 */
.username-cell,
.nickname-cell,
.dept-cell,
.mobile-cell,
.time-cell {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
}

.wework-cell {
  display: flex;
  justify-content: center;
}

.status-cell {
  display: flex;
  justify-content: center;
}

.cell-icon {
  font-size: 14px;
  flex-shrink: 0;
  color: $primary-color;
}

.cell-text {
  color: $text-regular;
  font-weight: 500;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

/* 企微绑定状态 */
.bind-status {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  transition: all 0.3s ease;

  &.bound {
    background: rgba(103, 194, 58, 0.1);
    color: $success-color;
    border: 1px solid rgba(103, 194, 58, 0.3);
  }

  &.unbound {
    background: rgba(230, 162, 60, 0.1);
    color: $warning-color;
    border: 1px solid rgba(230, 162, 60, 0.3);
    cursor: pointer;

    &:hover {
      background: rgba(230, 162, 60, 0.2);
      transform: scale(1.05);
    }
  }
}

.status-icon {
  font-size: 12px;

  &.success {
    color: $success-color;
  }

  &.warning {
    color: $warning-color;
  }
}

.status-text {
  font-size: 12px;
}

/* 开关样式 */
.enhanced-switch {
  :deep(.el-switch__core) {
    background-color: $danger-color;
    border-color: $danger-color;

    &.is-checked {
      background-color: $primary-color;
      border-color: $primary-color;
    }
  }
}

/* 操作按钮 */
.action-cell {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;

  @media (max-width: 768px) {
    flex-direction: column;
    gap: 4px;
  }
}

.action-button {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  transition: all 0.3s ease;

  &:hover {
    transform: scale(1.05);
  }

  .btn-icon {
    font-size: 12px;
  }

  @media (max-width: 768px) {
    padding: 4px 8px;
    font-size: 11px;
    width: 100%;
    justify-content: center;
  }

  &.edit-btn:hover {
    background: rgba(0, 82, 204, 0.1);
    color: $primary-color;
  }

  &.role-btn:hover {
    background: rgba(103, 194, 58, 0.1);
    color: $success-color;
  }

  &.more-btn:hover {
    background: rgba(144, 147, 153, 0.1);
    color: $info-color;
  }
}

.more-dropdown {
  @media (max-width: 768px) {
    width: 100%;
  }
}

/* 下拉菜单样式 */
.enhanced-dropdown-menu {
  border-radius: $border-radius-small;
  box-shadow: $shadow-light;
  border: 1px solid $border-light;
  overflow: hidden;

  .dropdown-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    transition: all 0.3s ease;

    .dropdown-icon {
      font-size: 14px;
    }

    &.delete-item {
      &:hover {
        background: rgba(245, 108, 108, 0.1);
        color: $danger-color;
      }
    }

    &.reset-item {
      &:hover {
        background: rgba(230, 162, 60, 0.1);
        color: $warning-color;
      }
    }
  }
}

/* 分页样式 */
.pagination-wrapper {
  display: flex;
  justify-content: center;
  padding: 20px 24px;
  background: white;
  border-top: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px 20px;
  }

  :deep(.el-pagination) {
    .el-pager li {
      border-radius: 6px;
      margin: 0 2px;
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light;
        color: $primary-color;
      }

      &.is-active {
        background: $primary-color;
        color: white;
      }
    }

    .btn-prev,
    .btn-next {
      border-radius: 6px;
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light;
        color: $primary-color;
      }
    }

    @media (max-width: 768px) {
      .el-pagination__sizes,
      .el-pagination__jump {
        display: none;
      }
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .user-management-container {
    background: #1f2937;
  }

  .dept-tree-wrapper,
  .search-wrapper,
  .table-wrapper {
    background: #374151;
    border-color: #4b5563;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  }

  .tree-container,
  .search-form-container,
  .table-container,
  .pagination-wrapper {
    background: #374151;
  }

  .tree-title,
  .search-title,
  .table-title {
    color: #f9fafb;
  }

  .cell-text {
    color: #f9fafb;
  }

  .enhanced-table {
    :deep(.el-table__header) {
      th {
        background: #4b5563 !important;
        color: #f9fafb !important;
        border-bottom-color: #6b7280;
      }
    }

    :deep(.el-table__cell) {
      border-bottom-color: #4b5563;
    }
  }

  .enhanced-input,
  .enhanced-select,
  .enhanced-date-picker {
    :deep(.el-input__wrapper),
    :deep(.el-select__wrapper) {
      background: #4b5563;
      border-color: #6b7280;

      .el-input__inner {
        color: #f9fafb;
      }
    }
  }

  .option-icon {
    &.success {
      color: #34d399;
    }

    &.danger {
      color: #f87171;
    }
  }

  .bind-status {
    &.bound {
      background: rgba(52, 211, 153, 0.1);
      color: #34d399;
      border-color: rgba(52, 211, 153, 0.3);
    }

    &.unbound {
      background: rgba(251, 191, 36, 0.1);
      color: #fbbf24;
      border-color: rgba(251, 191, 36, 0.3);

      &:hover {
        background: rgba(251, 191, 36, 0.2);
      }
    }
  }

  .enhanced-dropdown-menu {
    background: #4b5563;
    border-color: #6b7280;

    .dropdown-item {
      color: #f9fafb;

      &.delete-item:hover {
        background: rgba(248, 113, 113, 0.1);
        color: #f87171;
      }

      &.reset-item:hover {
        background: rgba(251, 191, 36, 0.1);
        color: #fbbf24;
      }
    }
  }
}

/* 响应式优化 */
@media (max-width: 480px) {
  .tree-header,
  .search-header,
  .table-header {
    padding: 12px 16px;
  }

  .tree-container,
  .search-form-container {
    padding: 12px;
  }

  .pagination-wrapper {
    padding: 12px 16px;
  }

  .enhanced-table {
    :deep(.el-table__header) th {
      padding: 10px 6px;
      font-size: 12px;
    }

    :deep(.el-table__body) td {
      padding: 10px 6px;
      font-size: 11px;
    }
  }

  .cell-text {
    font-size: 11px;
  }

  .action-button {
    padding: 3px 6px;
    font-size: 10px;
  }

  .bind-status {
    font-size: 10px;
    padding: 2px 6px;
  }

  .status-text {
    font-size: 10px;
  }
}
</style>
